package com.drojian;

import android.content.Context;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;

public class response implements Callable<StringBuilder> {
    Context context;
    String fileName;

    public response(Context context, String fileName) {
        this.context = context;
        this.fileName = fileName;
    }

    @Override
    public StringBuilder call(){
        List<DataItem> data = readAndParseJson(context, "keyword.json");
        StringBuilder builder = new StringBuilder();
        for (DataItem item : data) {
            builder.append("id="+item.getId()+" name="+item.getName()+"\n");

        }
        return builder;
    }
    public static List<DataItem> readAndParseJson(Context context, String fileName) {
        File externalFilesDir = context.getExternalFilesDir(null);
        File jsonFile = new File(externalFilesDir, fileName);
        List<DataItem> data = null;
        try (BufferedReader reader = new BufferedReader(new FileReader(jsonFile))) {
            Gson gson = new Gson();
            Root root = gson.fromJson(reader, Root.class);
            data =  root.getData();
            return data;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }
}
